package emstest;

import com.sss.simpleemps.common.domain.Admin;
import com.sss.simpleemps.common.utils.HibernateUtils;
import com.sss.simpleemps.dao.AdminDao;
import com.sss.simpleemps.dao.AdminDaoImpl;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;

public class DaoTest {
    @Test
    public void test1(){
        //获取Session
        /*Configuration configuration=new Configuration().configure("hibernate.cfg.xml");
        ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        SessionFactory sessionFactory=configuration.buildSessionFactory(serviceRegistry);
        Session session = sessionFactory.openSession();
        Admin admin =(Admin) session.get(Admin.class, 1);
        System.out.println(admin);*/
        Session session= HibernateUtils.getSession();
        System.out.println(session.hashCode());
        System.out.println(HibernateUtils.getSession().hashCode());
        System.out.println(HibernateUtils.getSession().hashCode());
        System.out.println(HibernateUtils.getSession().hashCode());
    }

    @Test
    public void test2(){
        Admin admin11=new Admin().setName("test3").setPassword("test11");
        Session session=HibernateUtils.getSession();
        HibernateUtils.beginSessionTransaction();
        session.save(admin11);
        HibernateUtils.commitAndCloseSession();
    }

    @Test
    public void test3(){
        AdminDao adminDao=new AdminDaoImpl();
        Admin admin =adminDao.selectAdminByName("test");
        System.out.println(admin);
    }
}
